/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller.c;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.entity.VOrder;
import com.ykht.proj.activity.entity.VOrderCard;
import com.ykht.proj.activity.handle.OrderHandler;
import com.ykht.proj.activity.service.VOrderService;
import com.ykht.proj.common.core.constant.SecurityConstants;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.coupon.api.feign.RemoteRewardPersonService;
import com.ykht.proj.coupon.vo.VOrderCardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 订单(企业 用户购买卡订单)表
 *
 * @author csg
 * @date 2021-05-21 14:22:31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("c/vorder" )
@Api(value = "vorder", tags = "c端订单")
@Slf4j
public class CVOrderController {

    private final  VOrderService vOrderService;
    private final  OrderHandler orderHandler;
    private final  RemoteRewardPersonService remoteRewardPersonService;

	@ApiOperation(value = "创建平台订单")
	@GetMapping("create/{cardId}")
	public R order(@PathVariable("cardId") Long cardId, @RequestParam(value = "shopIds",required = false) String shopIds,Long shareId) {
		log.info("->结算生成订单,id:	{},shopIds:{}", cardId, shopIds);
		return vOrderService.order(cardId, shopIds,shareId);
	}
	@ApiOperation(value = "支付下单")
	@GetMapping("pay/{orderId}")
	public R pay(@PathVariable("orderId") Long orderId) {
		log.info("->支付生成加密数据,orderId:	{}", orderId);
		return orderHandler.pay(orderId);
	}

	@ApiOperation(value = "支付异步回调")
	@PostMapping("callback")
	@Inner(false)
	public R callback(@RequestBody String data, HttpServletRequest request) {
		log.info("->支付回调request:{}", request);
		log.info("->支付回调data:{}", data);
		return vOrderService.toCallback(data, request);
	}


	@GetMapping("test")
	@Inner(false)
	public R test() {
		VOrderCard vOrderCard = new VOrderCard().selectById(1350L);
		remoteRewardPersonService.rewardCard(BeanUtil.toBean(vOrderCard, VOrderCardVO.class), SecurityConstants.FROM_IN);
		return R.ok();
	}



}
